package com.model.dao;

// Generated Oct 10, 2013 9:06:03 PM by Hibernate Tools 4.0.0

import java.util.List;

import javax.ejb.Stateless;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 * Home object for domain model class OffersTable.
 * @see com.model.dao.OffersTable
 * @author Hibernate Tools
 */
@Stateless
public class OffersTableDAO {

	private static final Log log = LogFactory.getLog(OffersTableHome.class);

	Session session;
	Transaction tx;

	public void persist(OffersTable transientInstance) throws Exception {
		log.debug("persisting OffersTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			session.persist(transientInstance);
			tx.commit();
			log.debug("persist successful");
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("persist failed", ex);
			throw ex;
		}
	}

	public void remove(OffersTable persistentInstance) throws Exception {
		log.debug("removing OffersTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			session.delete(persistentInstance);
			tx.commit();
			log.debug("remove successful");
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("remove failed", ex);
			throw ex;
		}
	}

	public OffersTable merge(OffersTable detachedInstance) throws Exception {
		log.debug("merging OffersTable instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			OffersTable result = (OffersTable) session
					.merge(detachedInstance);
			tx.commit();
			log.debug("merge successful");
			return result;
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("merge failed", ex);
			throw ex;
		}
	}

	public OffersTable findById(int id) {
		log.debug("getting OffersTable instance with id: " + id);
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			OffersTable instance = (OffersTable) session.get(
					MastercatalogTable.class, id);
			tx.commit();
			log.debug("findById successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("findById failed", re);
			throw re;
		}
	}
	@SuppressWarnings("unchecked")
	public List<OffersTable> findByAll() throws Exception {
		log.debug("getting OffersTable instance ");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			Query query = session.createQuery("from OffersTable where active='Y' ");
			List<OffersTable> list = query.list();
			tx.commit();
			log.debug("findById successful");
			return list;
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("findById failed", ex);
			throw ex;
		}
	}
}
